# 爬取电影天堂信息

import requests
import re

domain = 'https://www.dytt89.com/'
resp = requests.get(domain, verify=False)  # verify=False  去掉安全验证
resp.encoding = 'gb2312'  # 指定字符集
# print(resp.text)
resp.close()

# 拿到 ul 里面的 li
obj1 = re.compile(r'\d{4}必看热片.*?<ul>(?P<ul>.*?)</ul>', re.S)
obj2 = re.compile(r"<a href='(?P<href>.*?)' title=.*?", re.S)
obj3 = re.compile(r'◎片　　名(?P<mname>.*?)<br />'
                  r'.*?<td style="WORD-WRAP: break-word" bgcolor="#fdfddf"><a href="(?P<downLoad>.*?)">', re.S)

result1 = obj1.finditer(resp.text)
child_href_list = []
for item in result1:
    ul = item.group('ul')

    # 提取子页面链接
    result2 = obj2.finditer(ul)
    for it in result2:
        # 拼接子页面的url 地址：域名 + 子页面地址
        child_href = domain + it.group('href').strip('/')
        child_href_list.append(child_href)  # 把子页面链接保存起来

print('----------------------------------------------------------------------------------------')

# 提取子页面内容
for href in child_href_list:
    child_resp = requests.get(href, verify=False)
    child_resp.encoding = 'gb2312'  # 指定字符集
    child_resp.close()
    # print(child_resp.text)
    result3 = obj3.search(child_resp.text)
    print(result3.group('mname'))
    print(result3.group('downLoad'))
    break  # 测试用
